from keras import layers
from keras import models






model = models.Sequential()

model.add(layers.Conv2D(8,(3,3),activation='relu',input_shape=(28,28,1)))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(16,(3,3),activation ='relu'))
model.add(layers.MaxPooling2D((2,2)))
model.add(layers.Conv2D(16,(3,3),activation='relu'))

model.add(layers.Flatten())


#model.add(layers.Dense(288,activation='relu'))

model.add(layers.Dense(62,activation='softmax'))


print (model.summary())


from keras.utils import to_categorical
import mydata


datasetdir = r'M:\dataset'

imgs,labels = mydata.load_data(datasetdir)


img_Quantity = imgs.shape[0]
label_Quantity = labels.shape[0]

train_Quantity = img_Quantity * 9 // 10
test_Quantity = img_Quantity * 1 // 10

print('train_Quantity= %d, test_Quantity=%d'%(train_Quantity,test_Quantity))

train_images = imgs[0:train_Quantity]
train_labels = labels[0:train_Quantity]

test_images = imgs[train_Quantity:train_Quantity+test_Quantity]
test_labels = labels[train_Quantity:train_Quantity+test_Quantity]


print( train_images.shape)
print( train_labels.shape)
print( test_images.shape)
print( test_labels.shape)


train_images = train_images.reshape((train_Quantity,28,28,1))
train_images = train_images.astype('float32')/255

test_images = test_images.reshape((test_Quantity,28,28,1))
test_images = test_images.astype('float32')/255

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

model.compile(optimizer='rmsprop',
               loss='categorical_crossentropy',
               metrics=['accuracy'])

model.fit(train_images,train_labels,epochs=5,batch_size=512)

model.save('./ascllmodel')